package com.example.tms.mapper;

import com.example.tms.bean.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * App司机注册信息
 */
@Repository
public interface DriverAMapper extends JpaRepository<DriverApp,String>, JpaSpecificationExecutor<DriverApp> {
    //查询司机信息（身份证号）
    DriverApp findBySfzh(String sfzh);

    //查询司机信息（身份证号）
    @Query(value = "select * from driver_app where sfzh = ?1 and shzt = ?2 and sfcz = true", nativeQuery = true)
    DriverApp findBySfzhandShzt(String sfzh,String Shzt);

    //查询司机信息（身份证号）
    @Query(value = "select * from driver_app where sfzh = ?1 and shzt = ?2 and sfgrzx = true and sfcz = true", nativeQuery = true)
    DriverApp findBySfzhandShztandSfgezx(String sfzh,String Shzt);

    //查询司机信息
    @Query(value = "select * from driver_app where sjid = ?1 and sfcz = true order by lasttime desc", nativeQuery = true)
    List<DriverApp> findBySjidandsfcz(String sjid);

    //查询司机信息
    @Query(value = "select * from driver_app where sfzh = ?1 and sfcz = true order by lasttime desc", nativeQuery = true)
    List<DriverApp> findBySfzhandsfcz(String sfzh);

    //查询司机信息
    @Query(value = "select * from driver_app where sjid = ?1 and sfcz = true and shzt = '审核通过' order by lasttime desc", nativeQuery = true)
    DriverApp findBySjidandsfczandShzt(String sjid);

    //查询司机信息（手机号码）
    DriverApp findBySjhm(String sjhm);

    @Query(value = "select * from driver_app where sjhm = ?1 and shzt = ?2 and sfcz = true", nativeQuery = true)
    DriverApp findBySjhmandShzt(String sjhm,String Shzt);

    //查询司机信息（身份证号）
    @Query(value = "select * from driver_app where sjhm = ?1 and shzt = ?2 and sfgrzx = true and sfcz = true", nativeQuery = true)
    DriverApp findBySjhmandShztandSfgezx(String sjhm,String Shzt);

    //查询车辆信息（车辆id）
    DriverApp findBySjidAndSfcz(String sjid, boolean sfcz);

    //查询车辆信息（车辆id）
    DriverApp findBySfzh(String sfzh, boolean sfcz);

    //更新司机信息
    @Query(value = "update driver_app set xinxi = ?1,sfcg = ?2 where sjid = ?3", nativeQuery = true)
    @Modifying
    @Transactional
    void updateBySjid(String xinxi, Boolean sfcg, String sjid);

    //审核用户注册
    @Query(value = "update driver_app set yuanyin = ?1,sfcg = ?2 ,shzt = ?3 where sjid = ?4", nativeQuery = true)
    @Modifying
    @Transactional
    void updateBySjidSh(String yuanyin, Boolean sfcg,String shzt, String sjid);

    Page<DriverApp> findAll(Specification<DriverApp> spec, Pageable pageable);

}
